﻿using System;
using System.Collections;
using System.Data;
using System.Reflection;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Xml;
using NHibernate;
using System.Drawing;
using VitCode.BaseDataPageCreator.Setting;
using VitCode.Castle.DataAccess;
using VitCode.Castle.Helper;
using VitCode.ObjectResolutionHelper;
using VitCode.Web.UI;
using VitCode.Web.UI.WebControls;

namespace VitCode.BaseDataPageCreator
{
    /// <summary>
    /// CostPageBuilder 的摘要说明。
    /// </summary>
    public class PageBuilder
    {
        public PageLoadHelper m_LoadHelper;
        private Page m_Page;
        private string m_SettingXmlPath;
        private ObjXmlReader m_XmlReader;

        public PageBuilder(Page page, string settingXmlPath)
        {
            m_SettingXmlPath = settingXmlPath;
            m_Page = page;
            m_LoadHelper = new PageLoadHelper(settingXmlPath);
            m_XmlReader = new ObjXmlReader(settingXmlPath);
        }

        public PropertyInfo GetProperty(string proName, string className)
        {
            return m_LoadHelper.GetProperty(proName, className);
        }

        public object GetPropertyValue(string proName, object obj)
        {
            return m_LoadHelper.GetPropertyValue(proName, obj);
        }

        public XmlNode GetClassNode(string className)
        {
            return m_XmlReader.GetClassXMLNode(className);
        }

        public string GetNodeAttributeValue(XmlNode node, string attribulteName, bool isIgnore)
        {
            return m_XmlReader.GetNodeAttributeValue(node, attribulteName, isIgnore);
        }

        public string GetDialogShowPropertyName(string keyContextValue)
        {
            return keyContextValue.Substring(0, keyContextValue.IndexOf(":"));
        }

        public string GetDialogKeyPropertyName(string keyContextValue)
        {
            return keyContextValue.Substring(0, keyContextValue.IndexOf(":"));
        }

        private void SetControlProperty(object control, string proName, object proValue)
        {
            PropertyInfo pi = control.GetType().GetProperty(proName);
            if (pi != null)
            {
                pi.SetValue(control, Convert.ChangeType(proValue, pi.PropertyType), null);
            }
        }

        public void SetControlProperties(object control, XmlNode node)
        {
            foreach (XmlAttribute att in node.Attributes)
            {
                if (att.Name.StartsWith("ctl_"))
                {
                    string proName = att.Name.Substring(att.Name.IndexOf("_") + 1);
                    SetControlProperty(control, proName, att.Value);
                }
            }
        }

        public void SetContralProInfo(Object ctl, XmlNode node)
        {
            string ctlProInfo = m_XmlReader.GetNodeAttributeValue(node, BaseDataKeyInfo.CTL_PRO_INFO, true);

            if (ctlProInfo.Length > 0)
            {
                string[] ss = ctlProInfo.Split(',');
                foreach (string str in ss)
                {
                    string[] strss = str.Split(':');
                    string proName = strss[0];
                    string proStringValue = strss[1];
                    PropertyInfo cp = ctl.GetType().GetProperty(proName);
                    object val;
                    if (cp.PropertyType.IsEnum)
                        val = Enum.Parse(cp.PropertyType, proStringValue, false);
                    else if (cp.PropertyType.Equals(typeof(Unit)))
                    {
                        val = Unit.Parse(proStringValue);
                    }
                    else
                        val = Convert.ChangeType(proStringValue, cp.PropertyType);
                    cp.SetValue(ctl, val, null);
                }
            }
        }

        public void SetContralAttrInfo(Object ctl, XmlNode node)
        {
            string ctlProInfo = m_XmlReader.GetNodeAttributeValue(node, BaseDataKeyInfo.CTL_ATTR_INFO, true);

            if (ctlProInfo.Length > 0)
            {
                string[] ss = ctlProInfo.Split(',');
                foreach (string str in ss)
                {
                    string[] strss = str.Split(':');
                    string attrName = strss[0];
                    string attrStringValue = strss[1];
                    if (ctl is TextBox)
                    {
                        ((TextBox)ctl).Attributes[attrName] = attrStringValue;
                    }
                }
            }
        }

        public void CreatePageRuleHiddenCtl(XmlNode proNode, string checkCtlId, ref HtmlTableCell cell)
        {
            XmlNodeList pageRuleNL = m_XmlReader.GetChildNodesFromXML(proNode, BaseDataKeyInfo.PAGE_RULE, true);
            for (int i = 0; pageRuleNL != null && i < pageRuleNL.Count; i++)
            {
                XmlNode pageRuleNode = pageRuleNL[i];
                var hd = new HtmlInputHidden();
                hd.ID = checkCtlId + "_" + BaseDataKeyInfo.PAGE_RULE + "_" + i;
                hd.Attributes["name"] = checkCtlId + "_" + BaseDataKeyInfo.PAGE_RULE;
                hd.Attributes["class"] = BaseDataKeyInfo.PAGE_RULE;
                string ruleTypeName = m_XmlReader.GetNodeAttributeValue(pageRuleNode, BaseDataKeyInfo.TYPE_NAME, true);
                if (ruleTypeName.Length > 0)
                    hd.Value = new PageRuleHelper(m_SettingXmlPath).GetPageRuleCtlInfoByTypeName(ruleTypeName);
                cell.Controls.Add(hd);
            }
        }

        private void SetCellControl(ref HtmlTableCell cell, PropertyInfo pi, XmlNode proNode)
        {
            cell.Width = "35%";
            string ctlId = BaseDataKeyInfo.OBJ_PRO_HEAD_STRING + pi.Name;
            try
            {
                Control c = null;
                string userCtlPath = m_XmlReader.GetNodeAttributeValue(proNode, BaseDataKeyInfo.USER_CTL_PATH, true);
                string ctlType = m_XmlReader.GetNodeAttributeValue(proNode, BaseDataKeyInfo.CTL_TYPE, true);
                string strFormatString = m_XmlReader.GetNodeAttributeValue(proNode, BaseDataKeyInfo.FORMAT_STRING, true);
                string strInitNow = m_XmlReader.GetNodeAttributeValue(proNode, BaseDataKeyInfo.INIT_NOW, true);


                if (userCtlPath.Length > 0)
                {
                    c = m_Page.LoadControl(userCtlPath);
                    c.ID = ctlId;
                    PropertyInfo proCheckCtlIds = c.GetType().GetProperty(BaseDataKeyInfo.CHECK_CTL_IDS);
                    if (proCheckCtlIds != null)
                    {
                        object proValObj = proCheckCtlIds.GetValue(c, null);
                        if (proValObj != null)
                        {
                            var proVal = (string[])proValObj;
                            foreach (string strCheckCtlId in proVal)
                            {
                                CreatePageRuleHiddenCtl(proNode, strCheckCtlId, ref cell);
                            }
                        }
                    }
                }
                else if (ctlType.Length > 0)
                {
                    Type cType = m_LoadHelper.GetType(ctlType);
                    if (cType != null)
                        c = (Control)Activator.CreateInstance(cType);
                }
                else
                {
                    if (pi.PropertyType == typeof(bool))
                        c = new CheckBox();
                    else if (pi.PropertyType == typeof(DateTime))
                    {
                        var txtDate = new TextBox();
                        if (strInitNow == "true")
                        {
                            txtDate.Text = DateTime.Now.ToString(strFormatString);
                        }
                        c = txtDate;
                    }
                    else
                    {
                        c = new TextBox();
                    }
                }
                if (c != null)
                {
                    c.ID = ctlId;
                    SetContralProInfo(c, proNode);
                    SetContralAttrInfo(c, proNode);
                    cell.Controls.Add(c);
                    CreatePageRuleHiddenCtl(proNode, ctlId, ref cell);
                }
            }
            catch (Exception ex)
            {
                m_XmlReader.ShowErrorSettingInfo(proNode, ex);
            }
        }

        private void SetTableRow(ref HtmlTableRow row, XmlNode node, string className)
        {
            Type objType = m_LoadHelper.GetClassTypeByClassNodeName(className);
            if (objType != null)
                SetTableRow(ref row, node, objType);
        }
        private void SetTableRow(ref HtmlTableRow row, XmlNode node, Type objType)
        {
            string strMemo;
            if (row == null)
                row = new HtmlTableRow();
            strMemo = m_XmlReader.GetNodeAttributeValue(node, BaseDataKeyInfo.MEMO, true);
            if (node.Attributes.Count == 0 && row.Cells.Count == 2)
            {
                row.Cells[1].ColSpan = 3;
                return;
            }
            if (strMemo.Length == 0)
                return;
            strMemo = m_XmlReader.GetNodeAttributeValue(node, BaseDataKeyInfo.MEMO, true);
            var cell1 = new HtmlTableCell();
            cell1.InnerText = strMemo;
            cell1.Width = "15%";
            cell1.Attributes["class"] = "tdPageObjProMemo";
            row.Cells.Add(cell1);
            PropertyInfo pi = m_LoadHelper.GetProperty(node, objType);
            var cell2 = new HtmlTableCell();
            cell2.Attributes["class"] = "tdPageObjProVal";
            SetCellControl(ref cell2, pi, node);
            row.Cells.Add(cell2);
        }



        public void SetTableInfoContext(HtmlTable table, string className)
        {
            SetTableInfoContext(table, className, null);
        }


        public void SetTableInfoContext(HtmlTable table, string className, string typeName)
        {
            XmlNodeList nodeList;
            if (typeName == null || typeName.Length == 0)
                nodeList = m_LoadHelper.GetProNodesFromXML(className);
            else
                nodeList = m_LoadHelper.GetProNodesFromXML(className, typeName);
            Type objType = m_LoadHelper.GetClassTypeByClassNodeName(className);
            if (objType != null)
                SetTableInfoContext(table, nodeList, objType);
        }

        public void SetTableInfoContext(HtmlTable table, XmlNodeList nodeList, Type objType)
        {
            HtmlTableRow row = null;
            for (int i = 0; i < nodeList.Count; i++)
            {
                XmlNode node = nodeList[i];
                SetTableRow(ref row, node, objType);
                if (i == nodeList.Count - 1 || i % 2 == 1)
                {
                    if (i == nodeList.Count - 1 && i % 2 == 0)
                    {
                        var cell3 = new HtmlTableCell();
                        row.Cells.Add(cell3);
                        var cell4 = new HtmlTableCell();
                        row.Cells.Add(cell4);
                    }
                    table.Rows.Add(row);
                    row = null;
                }
            }
        }

        public string GetPageKeyField(string className)
        {
            return m_LoadHelper.SetObjectKeyField(className);
        }

        public void SetPageInfoContext(HtmlTable table, string className)
        {
            SetPageInfoContext(table, className, "");
        }

        public void SetPageInfoContext(HtmlTable table, string className, string typeName)
        {
            SetTableInfoContext(table, className, typeName);
            XmlNode classTypeNode;
            if (typeName == null || typeName.Length == 0)
                classTypeNode = m_XmlReader.GetClassXMLNode(className);
            else
                classTypeNode = m_XmlReader.GetClassTypeNameXMLNode(className, typeName);
            string strMemo = GetNodeAttributeValue(classTypeNode, BaseDataKeyInfo.MEMO, true);
            SetPageTitle(strMemo + "编辑");
        }

        public void SetPageTitle(string titleName)
        {
            object ctl = m_Page.FindControl("pageTitle");
            if (ctl != null && ctl is HtmlGenericControl)
            {
                ((HtmlGenericControl)ctl).InnerText = titleName;
            }
        }

        public string GetPgListSqlValue(string className)
        {
            return GetPgListSqlValue(className, null);
        }

        public string GetPgListSqlValue(string className, string typeName)
        {
            return GetPgListSqlValue(className, typeName, BaseDataKeyInfo.LIST_SQL);
        }

        public string GetPgListSqlValue(string className, string typeName, string listSqlNodeName)
        {

            XmlNode listSqlNode = GetPgListSqlNode(className, typeName, listSqlNodeName);
            string sql = m_XmlReader.GetNodeAttributeValue(listSqlNode, BaseDataKeyInfo.SQL);
            return sql;
        }

        public XmlNode GetPgListSqlNode(string className, string typeName, string listSqlNodeName)
        {
            XmlNode classTypeNode;
            if (string.IsNullOrEmpty(typeName))
                classTypeNode = m_XmlReader.GetClassXMLNode(className);
            else
                classTypeNode = m_XmlReader.GetClassTypeNameXMLNode(className, typeName);
            XmlNode listSqlNode = classTypeNode.SelectSingleNode(listSqlNodeName);
            return listSqlNode;
        }

        public void SetPgListAttribute(PaginationGrid pgList, string className)
        {
            SetPgListAttribute(pgList, className, null);
        }

        public void SetPgListAtrrForSltPage(PaginationGrid pgList, string className, string typeName)
        {
            SetPgListAttribute(pgList, className, typeName, true);
        }

        public void SetPgListAttribute(PaginationGrid pgList, string className, string typeName)
        {
            SetPgListAttribute(pgList, className, typeName, false);
        }



        public void SetPgListAttribute(PaginationGrid pgList, string className, string typeName, bool isSelectPage)
        {
            XmlNode classTypeNode;
            if (string.IsNullOrEmpty(typeName))
                classTypeNode = m_XmlReader.GetClassXMLNode(className);
            else
                classTypeNode = m_XmlReader.GetClassTypeNameXMLNode(className, typeName);
            string strMemo = GetNodeAttributeValue(classTypeNode, BaseDataKeyInfo.MEMO, true);

            if (isSelectPage)
                SetPageTitle(strMemo + "列表");
            else
                SetPageTitle(strMemo + "选择页面");
            XmlNode listSqlNode = classTypeNode.SelectSingleNode(BaseDataKeyInfo.LIST_SQL);
            if(listSqlNode!=null)
            {
                string strPgListIsShowEditInfo = GetNodeAttributeValue(listSqlNode, BaseDataKeyInfo.PG_LIST_IS_SHOW_EDIT_INFO, true);
                bool pgListIsShowEditInfo = strPgListIsShowEditInfo.ToLower() != "false";
                if (CheckQueryType(className))
                    SetPgListAttribute(pgList, listSqlNode, pgListIsShowEditInfo, !isSelectPage, isSelectPage, true);
                else
                    SetPgListAttribute(pgList, listSqlNode, pgListIsShowEditInfo, !isSelectPage, isSelectPage);
            }
            


        }

        private void SetPgListAttribute(PaginationGrid pgList, XmlNode listSqlNode, bool isEdit, bool isSelect, bool isSelectObjPage, bool isQuery)
        {
            string sql = m_XmlReader.GetNodeAttributeValue(listSqlNode, BaseDataKeyInfo.SQL);
            var acc = new BaseDataAccess();
            var sqlHlp = new SqlHelper();
            acc.BeginTransaction();
            DataSet ds;
            try
            {
                sql = sql.Replace("{0}", "");
                ds = sqlHlp.ExecuteDataSet(acc.Session, sql + " where 1=2 ");
            }
            finally
            {
                acc.CloseSession();
            }
            pgList.AutoGenerateColumns = false;
            SetControlProperties(pgList, listSqlNode);

            pgList.Columns.Clear();


            if (isEdit)
            {

                var btnCol = new PaginationGridButtonColumn();
                btnCol.Text = "";
                btnCol.HeaderText = isSelectObjPage ? "选择" : "工单明细";
                btnCol.Text = "<img src='/Images/edit.gif' border=0>";
                btnCol.CommandName = "Select";
                btnCol.CommandType = PaginationGridButtonColumn.PaginationGridCommandType.Select;
                pgList.Columns.Add(btnCol);


                var btnCol2 = new PaginationGridButtonColumn();
                btnCol2.Text = "";
                btnCol2.HeaderText = isSelectObjPage ? "选择" : "资产明细";
                btnCol2.Text = "<img src='/Images/edit.gif' border=0>";
                btnCol2.CommandName = "Select";
                btnCol2.CommandType = PaginationGridButtonColumn.PaginationGridCommandType.Select;
                pgList.Columns.Add(btnCol2);
            }

            for (int i = 0; i < ds.Tables[0].Columns.Count; i++)
            {
                DataColumn col = ds.Tables[0].Columns[i];
                if (col.ColumnName != pgList.DataKeyField && col.ColumnName != "UserGroupId")
                {
                    var boundCol = new PaginationGridBoundColumn(col.ColumnName, col.ColumnName);
                    pgList.Columns.Add(boundCol);
                }
            }
        }

        public void SetPgListAttribute(PaginationGrid pgList, XmlNode listSqlNode, bool isEdit, bool isSelect, bool isSelectObjPage)
        {
            string sql = m_XmlReader.GetNodeAttributeValue(listSqlNode, BaseDataKeyInfo.SQL);
            var acc = new BaseDataAccess();
            var sqlHlp = new SqlHelper();
            acc.BeginTransaction();
            DataSet ds;
            try
            {
                sql = sql.Replace("{0}", "");
                ds = sqlHlp.ExecuteDataSet(acc.Session, sql + " where 1=2 ");
            }
            finally
            {
                acc.CloseSession();
            }
            pgList.AutoGenerateColumns = false;
            SetControlProperties(pgList, listSqlNode);

            pgList.Columns.Clear();
            if (isSelect)
            {
                var checkCol = new PaginationGridCheckColumn();
                checkCol.ShowLabel = false;
                checkCol.ShowHeadCheckBox = true;
                checkCol.DataField = pgList.DataKeyField;
                pgList.Columns.Add(checkCol);
            }

            if (isEdit)
            {

                var btnCol = new PaginationGridButtonColumn();
                btnCol.Text = "";
                btnCol.HeaderText = isSelectObjPage ? "选择" : "明细";
                btnCol.Text = "<img src='/Images/edit.gif' border=0>";
                btnCol.CommandName = "Select";
                btnCol.CommandType = PaginationGridButtonColumn.PaginationGridCommandType.Select;
                pgList.Columns.Add(btnCol);


            }

            for (int i = 0; i < ds.Tables[0].Columns.Count; i++)
            {
                DataColumn col = ds.Tables[0].Columns[i];
                if (col.ColumnName != pgList.DataKeyField && col.ColumnName != "UserGroupId")
                {

                    var boundCol = new PaginationGridBoundColumn(col.ColumnName, col.ColumnName);
                    //if (col.DataType.Equals(typeof(DateTime)))
                    //    boundCol.DataFormatString = "{0:yyyy-MM-dd}";
                    pgList.Columns.Add(boundCol);
                }
            }
        }

        private bool CheckQueryType(string className)
        {
            var nodeName = className;
            if (nodeName == "Consuming" || nodeName == "Maintain" || nodeName == "Borrow" || nodeName == "Return" || nodeName == "BackPosition"
                || nodeName == "Allocation" || nodeName == "Scrap" || nodeName == "Damage" || nodeName == "CatValue" || nodeName == "IsUsed")
                return true;
            return false;
        }

        public object GetObjectByHql(string className, string keyField, string keyValue)
        {
            return GetObjectByHql(className, keyField, keyValue, new BaseDataAccess().Session);
        }

        public object GetObjectByHql(string className, string keyField, string keyValue, ISession session)
        {
            PropertyInfo pi = GetProperty(keyField, className);
            if (string.IsNullOrEmpty(keyValue))
            {
                if (pi.PropertyType == typeof(Guid))
                    keyValue = Guid.Empty.ToString();
            }
            string hql = "From " + className + " c where c." + keyField + "='" + keyValue + "'";
            IList list = session.CreateQuery(hql).List();
            if (list != null && list.Count > 0)
                return list[0];
            return null;
        }

        public object SetPageData(string className, string typeName, string keyFieldValue)
        {
            string keyField = GetPageKeyField(className);
            object obj = GetObjectByHql(className, keyField, keyFieldValue);
            SetPageData(className, typeName, obj);
            return obj;
        }

        public void SetPageData(string className, string typeName, object pageObj)
        {
            if (pageObj != null)
            {
                XmlNodeList proNodes;
                if (typeName.Length > 0)
                    proNodes = m_LoadHelper.GetProNodesFromXML(className, typeName);
                else
                    proNodes = m_LoadHelper.GetProNodesFromXML(className);
                bool isUse = true;
                var strIsUse = m_XmlReader.GetNodeAttributeValue(className, typeName, BaseDataKeyInfo.TIMING_IS_USE_PRO_NAME, true);
                if (!string.IsNullOrEmpty(strIsUse))
                {
                    var isUsePro = pageObj.GetType().GetProperty(strIsUse);
                    if (isUsePro != null)
                    {
                        var proVal = isUsePro.GetValue(pageObj, null);
                        if (proVal is Boolean) isUse = Convert.ToBoolean(proVal);
                    }
                }
                foreach (XmlNode proNode in proNodes)
                {
                    SetPageChildControlData(pageObj, proNode, isUse);
                }
            }
        }

        public void SetPageChildControlData(object obj, XmlNode proNode, bool isUse)
        {
            try
            {
                string strName = GetNodeAttributeValue(proNode, BaseDataKeyInfo.NAME, true);
                if (strName.Length == 0)
                    return;
                string strKeyPro = GetNodeAttributeValue(proNode, BaseDataKeyInfo.CTL_OBJ_KEY_PAO, true);
                string strShowPro = GetNodeAttributeValue(proNode, BaseDataKeyInfo.CTL_OBJ_SHOW_PAO, true);
                string strShowPro2 = GetNodeAttributeValue(proNode, BaseDataKeyInfo.CTL_OBJ_SHOW_PAO_2, true);
                string strCondition = GetNodeAttributeValue(proNode, BaseDataKeyInfo.CONDITION_STRING, true);


                string strFormatString = GetNodeAttributeValue(proNode, BaseDataKeyInfo.FORMAT_STRING, true);

                Control c = m_Page.FindControl(BaseDataKeyInfo.OBJ_PRO_HEAD_STRING + strName);
                string ctlProNameForObj = m_XmlReader.GetNodeAttributeValue(proNode,
                                                                            BaseDataKeyInfo.CTL_PROPERTY_FRO_OBJCET,
                                                                            true);
                if (strKeyPro.Length > 0)
                {
                    SetControlData(obj, strName, c, strKeyPro, strShowPro, strShowPro2,strCondition, strFormatString, isUse );
                }
                else
                {
                    if (ctlProNameForObj == "SelectedDate")
                    {
                        PropertyInfo tempPi = c.GetType().GetProperty("InitDate");
                        if (tempPi != null && tempPi.PropertyType == typeof(DateTime))
                            ctlProNameForObj = tempPi.Name;
                    }

                    if (ctlProNameForObj.Length == 0)
                    {
                        if (c.GetType() == typeof(CheckBox))
                            ctlProNameForObj = "Checked";
                        else
                            ctlProNameForObj = "Text";
                    }
                    SetControlData(obj, strName, c, ctlProNameForObj, strFormatString, isUse);
                }
            }
            catch (Exception ex)
            {
                m_XmlReader.ShowErrorSettingInfo(proNode, ex);
            }
        }


        public void SetControlData(object obj, string objProName, object ctl, string ctlProName, string formatString, bool isUse)
        {
            if (obj != null)
            {
                PropertyInfo childPi = obj.GetType().GetProperty(objProName);
                object cProValue = childPi.GetValue(obj, null);
                if (cProValue == null)
                    return;
                if (cProValue.GetType() == typeof(DateTime))
                {
                    if (String.IsNullOrEmpty(formatString))
                        formatString = "yyyy-MM-dd HH:mm:ss";//Young加了时分秒
                    cProValue = ((DateTime)cProValue).ToString(formatString);
                }
                PropertyInfo tempPi = ctl.GetType().GetProperty(ctlProName);
                if (cProValue is Guid)
                    cProValue = cProValue.ToString();

                tempPi.SetValue(ctl, Convert.ChangeType(cProValue, tempPi.PropertyType), null);

                string classKeyProName = m_LoadHelper.GetClassKeyPropertyName(obj.GetType().Name);
                if (!isUse || (classKeyProName != null && classKeyProName == objProName))
                    m_LoadHelper.SetProperty(ctl, "Enabled", false);
            }
        }

        public void SetControlData(object obj, string objProName, object ctl, string ctlKeyProName,
                                   string ctlShowProName, string ctlShowProName2,string strCondition, string formatString, bool isUse)
        {
            if (obj != null)
            {
                PropertyInfo childPi = obj.GetType().GetProperty(objProName);
                object cProValue = childPi.GetValue(obj, null);
                if (cProValue == null)
                    return;
                if (cProValue.GetType() == typeof(DateTime))
                    cProValue = ((DateTime)cProValue).ToString(formatString);
                PropertyInfo tempPi = ctl.GetType().GetProperty(ctlKeyProName);
                tempPi.SetValue(ctl, cProValue, null);
                if (ctlShowProName.Length > 0)
                {
                    tempPi = ctl.GetType().GetProperty(ctlShowProName);
                    tempPi.SetValue(ctl, cProValue, null);
                }
                if (ctlShowProName2.Length > 0)
                {
                    tempPi = ctl.GetType().GetProperty(ctlShowProName2);
                    tempPi.SetValue(ctl, cProValue, null);
                }
                if( !string.IsNullOrEmpty(strCondition))
                {
                    tempPi = ctl.GetType().GetProperty(BaseDataKeyInfo.CONDITION_STRING);
                    tempPi.SetValue(ctl, strCondition, null);
                }
                string classKeyProName = m_LoadHelper.GetClassKeyPropertyName(obj.GetType().Name);
                if (!isUse || (classKeyProName != null && classKeyProName == objProName))
                    m_LoadHelper.SetProperty(ctl, "Enabled", false);
            }
        }

        private string GetKeyWordFromString(string str, string fgStr)
        {
            int firstIndex = str.IndexOf(fgStr);
            if (firstIndex > -1)
            {
                int seIndex = str.IndexOf(fgStr, firstIndex + 1);
                if (seIndex > -1 && seIndex == str.LastIndexOf(fgStr))
                    return str.Substring(firstIndex + fgStr.Length, seIndex - firstIndex);
            }
            return "";
        }

        public string GetErrorMsg(string str, Type objType)
        {
            string reVal = str;
            string strMemo = "";
            if (objType != null)
            {
                string reLateObjName = GetKeyWordFromString(str, BaseDataAccess.ERR_OBJ_NAME_FG);
                if (reLateObjName.Length > 0)
                {
                    strMemo = m_XmlReader.GetNodeAttributeValue(reLateObjName, BaseDataKeyInfo.MEMO, true);
                    if (strMemo.Length > 0)
                    {
                        reVal =
                            reVal.Replace(
                                BaseDataAccess.ERR_OBJ_NAME_FG + reLateObjName + BaseDataAccess.ERR_OBJ_NAME_FG, strMemo);
                    }
                }
                string nodeName = GetKeyWordFromString(str, BaseDataAccess.ERR_PRO_NAME_FG);
                if (nodeName.Length > 0)
                {
                    XmlNode n = m_XmlReader.GetXmlNode(objType.Name, null, nodeName);
                    if (strMemo.Length > 0)
                    {
                        reVal =
                            reVal.Replace(
                                BaseDataAccess.ERR_PRO_NAME_FG + reLateObjName + BaseDataAccess.ERR_PRO_NAME_FG, strMemo);
                    }
                }
            }


            return reVal;
        }

        public void ShowModalDialog(string urlVal, Page currPage, string strClassName, string strTypeName)
        {
            string strClassMemo = m_XmlReader.GetNodeAttributeValue(strClassName, strTypeName, BaseDataKeyInfo.MEMO, true);
            Window win = new Window(currPage, strClassMemo + "明细");
            win.Width = 800;
            win.Height = 500;
            win.ShowModalDialog(urlVal + "&IsNav=false");
        }
    }
}
